package com.mazaiting.classfile.attr.entity;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.constant.ConstantPool;
import lombok.Getter;

import java.util.Arrays;

/**
 * 注解实体
 *
 * @author mazaiting
 * @since 2021/8/13
 */
@Getter
public final class AnnotationsEntity {
    /**
     * 常量池
     */
    private final ConstantPool pool;
    /**
     * 类型索引 无符号 u2
     * must be a valid index into the constant_pool table
     */
    private final int typeIndex;
    /**
     * 节点对数量 无符号 u2
     */
    private final int numElementValuePairs;
    /**
     * 节点对数组
     */
    private final ElementValuePairsEntity[] elementValuePairs;

    public AnnotationsEntity(ClassReader reader, ConstantPool pool) {
        this.pool = pool;
        this.typeIndex = reader.readU2ToUnsignedShort();
        this.numElementValuePairs = reader.readU2ToUnsignedShort();
        this.elementValuePairs = new ElementValuePairsEntity[numElementValuePairs];
        for (int i = 0; i < numElementValuePairs; i++) {
            this.elementValuePairs[i] = new ElementValuePairsEntity(reader, pool);
        }
    }

    /**
     * 类型
     * @return 类型
     */
    public String getType() {
        return pool.getUtf8(typeIndex);
    }

    @Override
    public String toString() {
        return "AnnotationsEntity{" +
                "typeIndex=" + typeIndex +
                ", numElementValuePairs=" + numElementValuePairs +
                ", elementValuePairs=" + Arrays.toString(elementValuePairs) +
                '}';
    }
}


















